package org.afs.gescol.core.service.implementations;

import org.afs.gescol.core.dao.interfaces.SubjectManagmentDao;
import org.afs.gescol.core.dto.subject.SubjectAssignmentDto;
import org.afs.gescol.core.model.data.SubjectAssignment;
import org.afs.gescol.core.service.interfaces.SubjectManagementServices;
import org.afs.gescol.core.service.util.MessageKeyUtil;
import org.scub.foundation.framework.base.exception.TechnicalException;
import org.scub.foundation.framework.base.mapping.util.MapperDozerBean;
import org.scub.foundation.framework.base.messagesource.MessageSourceUtil;

public class SubjectManagementServicesImpl implements SubjectManagementServices {

    private MessageSourceUtil messageSourceUtil;

    private MapperDozerBean mapperDozerBean;

    private SubjectManagmentDao subjectAssignmentDao;

    @Override
    public SubjectAssignmentDto createOrUpdateSubjectAssignment(SubjectAssignmentDto assignment) {
        checkSubjectAssignment(assignment);
        SubjectAssignment managedAssignment = null;
        if (assignment.getAssignmentId() != null) {
            managedAssignment = updateSubjectAssignment(assignment);
        } else {
            managedAssignment = createSubjectAssignment(assignment);
        }
        return (SubjectAssignmentDto) mapperDozerBean.map(managedAssignment, SubjectAssignmentDto.class);
    }

    @Override
    public void removeSubjectAssignment(Long subjectAssignmentId) {
        final SubjectAssignment dataFromDb = getData(subjectAssignmentId);
        subjectAssignmentDao.deleteSubjectAssignment(dataFromDb);
    }

    private SubjectAssignment getData(Long subjectAssignmentId) {
        final SubjectAssignment dataFromDb = subjectAssignmentDao.getSubjectAssignmentById(subjectAssignmentId);
        if (dataFromDb == null) {
            throw new TechnicalException(messageSourceUtil.get(MessageKeyUtil.ERROR_NOT_FOUND_ASSIGNMENT_SUBJECT));
        }
        return dataFromDb;
    }

    private SubjectAssignment createSubjectAssignment(SubjectAssignmentDto assignment) {
        final SubjectAssignment dataToDb = new SubjectAssignment();
        mapperDozerBean.map(assignment, dataToDb);
        subjectAssignmentDao.createSubjectAssignment(dataToDb);
        return dataToDb;
    }

    private SubjectAssignment updateSubjectAssignment(SubjectAssignmentDto assignment) {
        final SubjectAssignment dataFromDb = getData(assignment.getAssignmentId());
        mapperDozerBean.map(assignment, dataFromDb);
        return dataFromDb;
    }

    private void checkSubjectAssignment(SubjectAssignmentDto assignment) {
        if (assignment == null) {
            throw new TechnicalException(messageSourceUtil.get(MessageKeyUtil.ERROR_NULL_ASSIGNMENT_SUBJECT));
        }
        // TODO Auto-generated method stub
        
    }

    /**
     * Set the value of messageSourceUtil.
     * @param messageSourceUtil the messageSourceUtil to set
     */
    public void setMessageSourceUtil(MessageSourceUtil messageSourceUtil) {
        this.messageSourceUtil = messageSourceUtil;
    }

    /**
     * Set the value of mapperDozerBean.
     * @param mapperDozerBean the mapperDozerBean to set
     */
    public void setMapperDozerBean(MapperDozerBean mapperDozerBean) {
        this.mapperDozerBean = mapperDozerBean;
    }

    /**
     * Set the value of subjectAssignmentDao.
     * @param subjectAssignmentDao the subjectAssignmentDao to set
     */
    public void setSubjectAssignmentDao(SubjectManagmentDao subjectAssignmentDao) {
        this.subjectAssignmentDao = subjectAssignmentDao;
    }

}
